<html>
<head><meta charset="utf-8"><title>Relying on the layout of &amp;dyn Trait · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html">Relying on the layout of &amp;dyn Trait</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="246246385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246246385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246246385">(Jul 16 2021 at 16:29)</a>:</h4>
<p>Can I rely on the memory layout of &amp;dyn Trait ? Especially the case of the pointer to the concrete type being the first one ?</p>
<p><code>Any</code> seems to rely on that (<a href="https://doc.rust-lang.org/src/core/any.rs.html#218">https://doc.rust-lang.org/src/core/any.rs.html#218</a>), I wonder if I can also do it...</p>



<a name="246246526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246246526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246246526">(Jul 16 2021 at 16:30)</a>:</h4>
<p>no, this is unspecified, the standard library can rely on it because it is tied to a specific compiler</p>



<a name="246246567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246246567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246246567">(Jul 16 2021 at 16:31)</a>:</h4>
<p>Thanks, that was my suspicious</p>



<a name="246248096"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246248096" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246248096">(Jul 16 2021 at 16:43)</a>:</h4>
<p>Is that a way to go from <code>Box&lt;dyn Any&gt;</code> to <code>Box&lt;T&gt;</code> ?</p>



<a name="246248383"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246248383" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246248383">(Jul 16 2021 at 16:45)</a>:</h4>
<p>is that actually relying on layout? I think <code>self as *const dyn Any as *const T</code> should be well-formed, and the compiler will translate the fat pointer to thin in the <code>as</code> cast.</p>



<a name="246248442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246248442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246248442">(Jul 16 2021 at 16:45)</a>:</h4>
<p>ah, right, that is fine of course</p>



<a name="246248538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246248538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246248538">(Jul 16 2021 at 16:46)</a>:</h4>
<p>Good catch</p>



<a name="246249251"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246249251" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246249251">(Jul 16 2021 at 16:52)</a>:</h4>
<p>So I can do</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="nb">Box</span>::<span class="n">from_raw</span><span class="p">(</span><span class="nb">Box</span>::<span class="n">into_raw</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="p">)</span>:
</code></pre></div>
<p>??</p>
<p>With <code>self</code> being <code>Box&lt;dyn Trait&gt;</code> and  after being checked that it's indeed <code>T</code>.</p>



<a name="246249757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246249757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246249757">(Jul 16 2021 at 16:56)</a>:</h4>
<p>I think so</p>



<a name="246249898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246249898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246249898">(Jul 16 2021 at 16:58)</a>:</h4>
<p>actually, isn't that just <a href="https://doc.rust-lang.org/std/boxed/struct.Box.html#method.downcast">https://doc.rust-lang.org/std/boxed/struct.Box.html#method.downcast</a> ?</p>



<a name="246250231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246250231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246250231">(Jul 16 2021 at 17:00)</a>:</h4>
<p>Thank you, I was sure something like that existed</p>



<a name="246250271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246250271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246250271">(Jul 16 2021 at 17:00)</a>:</h4>
<p>That only works for dyn Any, not any other dyn Trait</p>



<a name="246250370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246250370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246250370">(Jul 16 2021 at 17:01)</a>:</h4>
<p>true</p>



<a name="246250382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246250382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thales Fragoso <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246250382">(Jul 16 2021 at 17:01)</a>:</h4>
<p>Yeah, it doesn't cover everything I want but helps me now</p>



<a name="246250446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246250446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246250446">(Jul 16 2021 at 17:02)</a>:</h4>
<p>there's crates on <a href="http://crates.io">crates.io</a> (don't recall names off hand) that let you extend a trait with any-like behavior</p>



<a name="246250473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying%20on%20the%20layout%20of%20%26dyn%20Trait/near/246250473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Relying.20on.20the.20layout.20of.20.26dyn.20Trait.html#246250473">(Jul 16 2021 at 17:02)</a>:</h4>
<p>other traits could have an <code>is_foo</code> to enable other downcasts</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>